//
//  QZKaiPingVC.m
//  shortvideoplay
//
//  Created by adam on 2024/7/31.
//

#import "QZKaiPingVC.h"
#import <AdSupport/AdSupport.h>
#import <BUAdSDK/BUAdSDK.h>
#import "DJNavigationController.h"
#import "QZTabbarController.h"
#import "AppDelegate.h"
#import "CSJADLoading.h"
#import <AppTrackingTransparency/AppTrackingTransparency.h>
#import "GDTNativeExpressAdView.h"
#import "GDTNativeExpressAd.h"
#import "GDTSplashAd.h"
#import "GDTSDKConfig.h"
#import "MMDrawerController.h"
#import "QZMyRightVC.h"

@interface QZKaiPingVC ()<BUSplashAdDelegate, BUSplashCardDelegate, BUSplashZoomOutDelegate,GDTSplashAdDelegate>
@property (strong, nonatomic)BUSplashAd * csj_splashAd;
@property (strong, nonatomic)GDTSplashAd * ylh_splashAd;
@property (strong, nonatomic)UIButton *custormSkipButton;
@end

@implementation QZKaiPingVC

- (void)showOldAddNavViewToMainOldView1{
    UIView * oldBgSS = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 0, 0)];
    oldBgSS.clipsToBounds = YES;
       UILabel * oldshowSSSSTip = [[UILabel alloc]init];
    oldshowSSSSTip.clipsToBounds = YES;
    oldshowSSSSTip.text = @"kass显示区";
    oldshowSSSSTip.textColor = ColorHex(@"#f0f0f0");
    oldshowSSSSTip.textAlignment = NSTextAlignmentCenter;
    oldshowSSSSTip.font = [UIFont systemFontOfSize:28];
    oldshowSSSSTip.hidden = YES;
    [self.view addSubview:oldshowSSSSTip];
    [self.view addSubview:oldBgSS];
    
    UIButton * sendBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreen_W-75, kStatusBarHeight+10, 60, 46)];
    sendBtn.layer.cornerRadius = 3;
    sendBtn.backgroundColor = [UIColor whiteColor];
    [sendBtn setTitle:@"发送" forState:UIControlStateNormal];
    [sendBtn setTitleColor:ColorHex(@"#333333") forState:UIControlStateNormal];
    sendBtn.titleLabel.font = [UIFont systemFontOfSize:16];
    [sendBtn addTarget:self action:@selector(sendTap) forControlEvents:UIControlEventTouchUpInside];
    [oldBgSS addSubview:sendBtn];
    
    UIView * sendBg = [[UIView alloc]initWithFrame:CGRectMake(15, kStatusBarHeight+66, KScreen_W-30, (KScreen_H-KSafeAreaBottomHeight-25-(kStatusBarHeight+66))/2)];
    sendBg.backgroundColor = [UIColor whiteColor];
    sendBg.layer.cornerRadius = 3;
    [oldBgSS addSubview:sendBg];
    
    UILabel * sendTip = [[UILabel alloc]initWithFrame:CGRectMake(0, (sendBg.frame.size.height-40)/2, sendBg.frame.size.width, 40)];
    sendTip.text = @"发送区";
    sendTip.textColor = ColorHex(@"#f0f0f0");
    sendTip.textAlignment = NSTextAlignmentCenter;
    sendTip.font = [UIFont systemFontOfSize:28];
    [sendBg addSubview:sendTip];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    KWeakSelf
    [weakSelf ADInit];
    
}
- (void)ADInit{
    KWeakSelf
    [CSJADLoading initAppCSJAD_successBlock:^(BOOL issuccess) {
        [CSJADLoading initAppYLHAD_successBlock:^(BOOL issuccess) {
            NSLog(@"%@===%@",@([CSJADLoading share].isCSJAdInit),@([CSJADLoading share].isYLHAdInit));
            if([CSJADLoading share].isCSJAdInit || [CSJADLoading share].isYLHAdInit){
                [weakSelf ADSplashAdRequest];
            }else{
                [weakSelf AdFishShowEnd];
            }
        }];
    }];
}
- (void)ADSplashAdRequest{
    [QZUserInfoModel adRequestType:^(NSInteger type) {
        if(type == 0){
            self.csj_splashAd = [[BUSplashAd alloc] initWithSlotID:@"889521826" adSize:CGSizeMake(KScreen_W, KScreen_H)];
            self.csj_splashAd.tolerateTimeout = 3;
            self.csj_splashAd.delegate = self;
            self.csj_splashAd.cardDelegate = self;
            self.csj_splashAd.zoomOutDelegate = self;
            [self.csj_splashAd loadAdData];
        }else{
            self.ylh_splashAd = [[GDTSplashAd alloc]initWithPlacementId:@"5141034871210899"];
            self.ylh_splashAd.delegate = self;
            self.ylh_splashAd.fetchDelay = 3;
            [self.ylh_splashAd loadFullScreenAd];
//            [self.ylh_splashAd showFullScreenAdInWindow:window withLogoImage:KImageNamed(@"ico_logo") skipView:customSkipView];
        }
    }];
}
- (void)AdFishShowEnd{
    if(self.isQiDong){
        DJNavigationController * nav = [[DJNavigationController alloc]initWithRootViewController:[QZTabbarController new]];
        nav.delegate = [DJBarDelegate share];
        AppDelegate * app = (AppDelegate *)[UIApplication sharedApplication].delegate;
        [app.window.rootViewController removeFromParentViewController];
        app.window.rootViewController = nav;
//        DJNavigationController * right_nav = [[DJNavigationController alloc]initWithRootViewController:[QZMyRightVC new]];
//        
//        MMDrawerController * drawerController = [[MMDrawerController alloc]initWithCenterViewController:nav rightDrawerViewController:right_nav];
//        [drawerController setShowsShadow:NO];
//        drawerController.maximumRightDrawerWidth = KScreen_W*0.6;
//        
//        [drawerController setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
//        [drawerController setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
//        app.window.rootViewController = drawerController;
    }else{
        [self dismissViewControllerAnimated:NO completion:nil];
    }
}
#pragma mark splashAD
- (void)splashAdLoadSuccess:(nonnull BUSplashAd *)splashAd {
    [SVProgressHUDTool dismiss];
    [splashAd showSplashViewInRootViewController:self];
    if (splashAd.hideSkipButton) { // 自定义跳过按钮
        [splashAd.splashView addSubview:self.custormSkipButton];
    }
}
// 自定义跳过按钮
- (UIButton *)custormSkipButton {
    if(_custormSkipButton == nil){
        _custormSkipButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_custormSkipButton setTitle:@"关闭" forState:UIControlStateNormal];
        [_custormSkipButton setTitleColor:[UIColor darkTextColor] forState:UIControlStateNormal];
        [_custormSkipButton addTarget:self action:@selector(skipButtonTapped:) forControlEvents:UIControlEventTouchUpInside];
        CGFloat safeBottomMargin = 12.0;
        _custormSkipButton.frame = CGRectMake(KScreen_W - 56 - 12, kStatusBarHeight+6, 50, 30);
        _custormSkipButton.userInteractionEnabled = NO;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(2.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self->_custormSkipButton.userInteractionEnabled = YES;
        });
    }
    return _custormSkipButton;
}
- (void)skipButtonTapped:(UIButton *)sender {
    [_csj_splashAd removeSplashView];
    [self AdFishShowEnd];
}
- (void)splashAdLoadFail:(nonnull BUSplashAd *)splashAd error:(BUAdError * _Nullable)error {
    [self pbu_logWithSEL:_cmd msg:@""];
    [self AdFishShowEnd];
}

- (void)splashAdRenderFail:(nonnull BUSplashAd *)splashAd error:(BUAdError * _Nullable)error {
    [self pbu_logWithSEL:_cmd msg:@""];
    [self AdFishShowEnd];
}


- (void)splashAdRenderSuccess:(nonnull BUSplashAd *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashAdWillShow:(nonnull BUSplashAd *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashAdDidShow:(nonnull BUSplashAd *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashAdDidClick:(nonnull BUSplashAd *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashAdDidClose:(nonnull BUSplashAd *)splashAd closeType:(BUSplashAdCloseType)closeType {
    [_csj_splashAd removeSplashView];
    [self AdFishShowEnd];
}

- (void)splashCardReadyToShow:(nonnull BUSplashAd *)splashAd {
    [splashAd showCardViewInRootViewController:self];
    
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashCardViewDidClick:(nonnull BUSplashAd *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashCardViewDidClose:(nonnull BUSplashAd *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashAdViewControllerDidClose:(BUSplashAd *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashDidCloseOtherController:(nonnull BUSplashAd *)splashAd interactionType:(BUInteractionType)interactionType {
    [self pbu_logWithSEL:_cmd msg:@""];
}


- (void)splashVideoAdDidPlayFinish:(nonnull BUSplashAd *)splashAd didFailWithError:(nullable NSError *)error {
    [self pbu_logWithSEL:_cmd msg:@""];
}


- (void)splashZoomOutViewDidClick:(nonnull BUSplashAd *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}


- (void)splashZoomOutViewDidClose:(nonnull BUSplashAd *)splashAd {
    [self pbu_logWithSEL:_cmd msg:@""];
}

- (void)splashZoomOutReadyToShow:(nonnull BUSplashAd *)splashAd {
    
    AppDelegate *app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    // 接入方法一：使用SDK提供动画接入
    if (splashAd.zoomOutView) {
        [splashAd showZoomOutViewInRootViewController:app.window.rootViewController];
    }
    [self pbu_logWithSEL:_cmd msg:@""];
}


- (void)pbu_logWithSEL:(SEL)sel msg:(NSString *)msg {
}
#pragma mark ylh 代理
- (void)splashAdSuccessPresentScreen:(GDTSplashAd *)splashAd{
    NSLog(@"===获得金额%ld",[splashAd eCPM]);
    UIView *customSkipView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 40, 40)]; // 设置跳过按钮的frame信息
    UIWindow *window = [[UIApplication sharedApplication] keyWindow];
    [splashAd showFullScreenAdInWindow:window withLogoImage:KImageNamed(@"ico_logo") skipView:customSkipView];
}
-(void)splashAdFailToPresent:(GDTSplashAd *)splashAd withError:(NSError *)error{
    
    [self AdFishShowEnd];
}
-(void)splashAdWillClosed:(GDTSplashAd *)splashAd{
    [self AdFishShowEnd];
}
- (void)splashAdDidDismissFullScreenModal:(GDTSplashAd *)splashAd{
    [self AdFishShowEnd];
}
- (void)splashAdClosed:(GDTSplashAd *)splashAd{
    [self AdFishShowEnd];
}
@end
